package com.example.foods.service.impl;

import com.example.foods.data.OrderItemMapper;
import com.example.foods.data.OrdersMapper;
import com.example.foods.entity.OrderItems;
import com.example.foods.entity.Orders;
import com.example.foods.service.OrderIService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderIService {

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private OrderItemMapper orderItemMapper;

    @Override
    public List<Orders> list() {
        return ordersMapper.list();
    }

    @Override
    public void onOrder(Integer userId) {
        //取出未完结订单
        List<OrderItems> orderItemsList = orderItemMapper.list();

        //生成订单
        Orders orders = new Orders();
        orders.setCstId(userId);
        orders.setOrderDate(new Date());
        orders.setOutOrderId(UUID.randomUUID().toString());
        orders.setOrderStatus(1);

        //计算总金额
        Float totalPrice = 0f;
        for(OrderItems orderItems : orderItemsList){
            if(orderItems.getPrice() != null){
                totalPrice = totalPrice + orderItems.getPrice();
            }
        }
        orders.setTotalPrice(totalPrice);

        //订单入库
        ordersMapper.save(orders);

        //todo 删除之前的购物项目
        for(OrderItems orderItems : orderItemsList){
//           ordersMapper.delete(orderItems.getOrderId());
        }
    }

    @Override
    public void delete(Integer id) {
        ordersMapper.delete(id);
    }
}
